package com.zmzncs.shop;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import com.github.xiaoymin.knife4j.spring.extension.OpenApiExtensionResolver;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.annotation.Order;
import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
import springfox.documentation.annotations.ApiIgnore;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.RequestParameterBuilder;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Configuration
@EnableOpenApi
@EnableSwagger2
@EnableKnife4j
@Import(BeanValidatorPluginsConfiguration.class)
@ApiIgnore
public class Swagger2Config {

    /*引入Knife4j提供的扩展类*/
    private final OpenApiExtensionResolver openApiExtensionResolver;

    public Swagger2Config(OpenApiExtensionResolver openApiExtensionResolver) {
        this.openApiExtensionResolver = openApiExtensionResolver;
    }

    @Bean(value = "park")
    public Docket park() {
        Docket docket=new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo())
                .groupName("1、停车场")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.zmzncs.shop.module.park.controller"))
                .paths(PathSelectors.any())
                .build()
                .ignoredParameterTypes(Page.class) //添加忽略类型
                .extensions(openApiExtensionResolver.buildExtensions("lmtc-shop"));
//                .securityContexts(getSecurityContexts())
//                .securitySchemes(getSecuritySchemes());

        return docket;
    }

    @Bean(value = "shop")
    public Docket shop() {
        Docket docket=new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo())
                .groupName("1、商户")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.zmzncs.shop.module.shop.controller"))
                .paths(PathSelectors.any())
                .build()
                .ignoredParameterTypes(Page.class) //添加忽略类型
                .extensions(openApiExtensionResolver.buildExtensions("lmtc-shop"));
//                .securityContexts(getSecurityContexts())
//                .securitySchemes(getSecuritySchemes());

        return docket;
    }

    private ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                .title("立码停车-商城")
                .description("<div style='font-size:14px;color:red;'>文档描述</div>")
                .termsOfServiceUrl("https://www.baidu.com/") //  服务条件
                .contact(new Contact("张大仙","https://baidu.com","953799106@qq.com"))
                .version("1.0")
                .build();
    }

    /**
     *  认证请求头
     */
    private List<SecurityScheme> getSecuritySchemes(){
        List<SecurityScheme> securitySchemes = Arrays.asList(
                new ApiKey("Authorization", "Authorization", "header")
        );

        return securitySchemes;
    }

    /**
     *  授权范围
     */
    private AuthorizationScope[] getAuthorizationScopes(){
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;

        return authorizationScopes;
    }

    /**
     *  认证目录
     */
    private List<SecurityContext> getSecurityContexts(){
        List<SecurityContext> securityContexts = Arrays.asList(
                SecurityContext.builder()
                        .securityReferences(Lists.newArrayList(new SecurityReference("Authorization", getAuthorizationScopes())))
                        .forPaths(PathSelectors.regex("/.*"))
                        .build()
        );

        return securityContexts;
    }

}

